<?php


function login(){
	$user = i('user','','post');
	$pass = i('pass','','post');
	if($user == '') to_ajax('0','101');
	if($pass == '') to_ajax('0','102');
	$sql = "SELECT * FROM ".$GLOBALS['dou']->table('user')." WHERE (user_name='{$user}' OR mobile='{$user}') AND password='".md5($pass)."' LIMIT 1";
	$arr = $GLOBALS['dou']->get_row($sql);
	if(empty($arr)) to_ajax(0, '103', $arr);
	to_ajax(1, '000', $arr);
}

function user_find_pass(){
	global $dou;
	$user_name = i('user_name','','post');
	$mobile    = i('mobile','','post');
	$sql  = "SELECT * FROM ".$dou->table('user')." WHERE (user_name='{$user_name}' AND mobile='{$mobile}') LIMIT 1";
	$user = $dou->get_row($sql);
	if(!$user) to_ajax(0, '108');

	$new_pass = coupon_no();
	$dou->query( $dou->update_sql($dou->table('user'), array('password'=>md5($new_pass)), "user_id='$user[user_id]'") );

	to_ajax(1, '000', array('new_pass'=>$new_pass));
}

function user_avatar(){
	/*$user_info = _user_info(i('usertoken','','post'));
	if(!$user_info) to_ajax(0, '108');*/

	// 图片上传
	include_once (ROOT_PATH . 'include/upload.class.php');
	$images_dir = 'images/avatar/'; // 文件上传路径，结尾加斜杠
	$img = new Upload(ROOT_PATH . $images_dir); // 实例化类文件
	if (!file_exists(ROOT_PATH . $images_dir))
	    mkdir(ROOT_PATH . $images_dir, 0777);
	if ($_FILES['avatar']['name'] != "")
        $avatar = $images_dir . $img->upload_image('avatar', $img->create_file_name('avatar'));
    to_ajax(1,'000', array('avatar'=>$avatar));
}

function apply(){
	global $dou;
	$param = array(
		'province'  => i('province','','post'),
		'city'      => i('city','','post'),
		'area'      => i('area','','post'),
		'contact'   => i('contact','','post'),
		'mobile'    => i('mobile','','post'),
		'email'     => i('email','','post'),
		'diner_num' => i('diner_num','','post'),
		'address'   => i('address','','post'),
		'need'      => i('need','','post'),
	);
	if($param['province'] == '')    to_ajax('0','301');
	if($param['city'] == '')    to_ajax('0','302');
	if($param['area'] == '')    to_ajax('0','303');
	if($param['contact'] == '') to_ajax('0','110');
	if($param['mobile'] == '')  to_ajax('0','305');
	if($param['diner_num'] == '') to_ajax('0','202');
	if($param['email'] == '')   to_ajax('0','107');
	if($param['address'] == '') to_ajax('0','304');
	$dou->query( $dou->insert_sql($dou->table('user_apply'), $param) );
	to_ajax(1,'000', $param);
}

function cat_list_son(){
	$parent_id = i('parent_id',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('product_category') . " WHERE parent_id='{$parent_id}' ORDER BY sort ASC";
	$arr['list'] = $GLOBALS['dou']->get_all($sql);
	to_ajax(1, '000', (array)$arr);
}
function cat_info(){
	$cat_id = i('cat_id',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('product_category') . " WHERE cat_id='{$cat_id}' LIMIT 1";
	$arr['cat_info'] = $GLOBALS['dou']->get_row($sql);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('product') . " WHERE cat_id='{$cat_id}'";
	$product_list = $GLOBALS['dou']->get_all($sql);
	$arr['list'] = array();
	if($product_list){
		foreach($product_list as $k=>$v){
			$arr['list'][$k]     = $v;
			$arr['list'][$k]['attr'] = _definded_array($v[defined]);
		}
	}
	to_ajax(1, '000', $arr);
}

function article_info(){
	$id = i('id',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('article') . " WHERE id='{$id}' LIMIT 1";
	$arr = $GLOBALS['dou']->get_row($sql);
	to_ajax(1, '000', $arr);
}
function page_list_son(){
	$parent_id = i('parent_id',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('page') . " WHERE parent_id='{$parent_id}' ORDER BY id DESC";
	$arr = $GLOBALS['dou']->get_all($sql);
	to_ajax(1, '000', $arr);
}
function page_info(){
	$id = i('id',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('page') . " WHERE id='{$id}' LIMIT 1";
	$arr = $GLOBALS['dou']->get_row($sql);
	to_ajax(1, '000', $arr);
}

function region_list_son(){
	$parent_id = i('parent_id',1);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('region') . " WHERE parent_id='{$parent_id}'";
	$arr['list'] = $GLOBALS['dou']->get_all($sql);
	to_ajax(1, '000', $arr);
}
function region_info(){
	$province = i('province',0);
	$city = i('city',0);
	$area = i('area',0);
	$sql = "SELECT * FROM " . $GLOBALS['dou']->table('region') . " WHERE region_id IN('$province','$city','$area')";
	$list = $GLOBALS['dou']->get_all($sql);
	$arr = array('province'=>'', 'city'=>'', 'area'=>'');
	foreach($list as $k=>$v){
		if($v['region_type'] == 1) $arr['province'] = $v['region_name'];
		if($v['region_type'] == 2) $arr['city'] = $v['region_name'];
		if($v['region_type'] == 3) $arr['area'] = $v['region_name'];
	}
	to_ajax(1, '000', $arr);
}

function server_time(){
	to_ajax(1, '000', array('timestamp'=>time(), 'year'=>date('Y'), 'month'=>date('m'), 'day'=>date('d') ));
}





/***------------------------------------------/
* 内部函数
* --------------------------------------------/
*/
function _definded_array($str){
	$arr = array();
	$definds = explode(',',$str);
	if( $str != ''){
		foreach($definds as $k=>$v){
		    $row            =  explode('：',$v);
		    $arr[] = array('key'=>$row[0], 'value'=>$row[1]);
		}
	}
	//print_r($arr);
	return $arr;
}
function _user_info($usertoken){
    $sql = "SELECT * FROM " . $GLOBALS['dou']->table('user') . " WHERE usertoken='{$usertoken}' LIMIT 1";
    $row = $GLOBALS['dou']->get_row($sql);
    if($row) return $row;
    return false;
}




